package lab;

/**
 * 卡号校验位计算
 * 规则可以自己定,table随意定,维度也不一定是2,可以增加,循环也可以正序循环.
 * 双方定好规则计算即可
 * @author Think
 *
 */
public class Lab16 {
	
	private static final int[][] table = { {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, {0, 2, 4, 6, 8, 1, 3, 5, 7, 9} };
	
	/**
	 * 获取校验位
	 */
	private static int checkDigit(String cardNo){
		char[] cardNum = cardNo.toCharArray();
		int cardLen = cardNo.length();
		 
		int i, odd, sum;
	    for (i = cardLen - 1, odd = 0, sum = 0; i >= 0; i--){
	        if (isDigit(cardNum[i])){
	            odd = 1 - odd;
	            sum += table[odd][cardNum[i] - '0'];
	        }
	    }

	    sum %= 10;
	    
	    return (sum > 0 ? 10 - sum : 0);
	}
	
	/**
	 * 是否是数字字符
	 */
	private static boolean isDigit(char c){
		if(c >= '0' && c <= '9'){
			return true;
		}else{
			return false;
		}
	}

	public static void main(String[] args) {
		String cardNo = "650001584000000092";
		System.out.println("卡号：" + cardNo);
		System.out.println("校验位：" + checkDigit(cardNo));
	}
}
